library(haven)
library(extraoperators)
library(psych)
library(dplyr)
library(interactions)

#Replicating baseline results for "Types of pandemic-induced psychological distress, clarity of responsibility, and support for incumbents"#

#Wave1#
Mental_health_wave_1_COVID_Eco_OB_MR_ <- read_sav("~/Downloads/Mental health wave-1 (COVID+Eco+OB+MR).sav")
coviddat1<-Mental_health_wave_1_COVID_Eco_OB_MR_
coviddat1<-as.data.frame(coviddat1)
coviddat1<-coviddat1[which(coviddat1$MHQ2a %gl% c(0, 3) & coviddat1$MHQ2b %gl% c(0, 3)& coviddat1$MHQ2c %gl% c(0, 3)& coviddat1$MHQ2d %gl% c(0, 3)& coviddat1$MHQ2e %gl% c(0, 3) & coviddat1$MHQ2f %gl% c(0, 3)& coviddat1$MHQ2g %gl% c(0, 3)& coviddat1$MHQ2h %gl% c(0, 3)&coviddat1$MHQ2i %gl% c(0, 3)& coviddat1$MHQ2j %gl% c(0, 3)),]
# Code for constructing dependent variable#
coviddat1$bjp2020<- ifelse(coviddat1$c10==12, 1, ifelse(coviddat1$c10==22, NA, ifelse(coviddat1$c10==28, NA, ifelse(coviddat1$c10==37, NA, ifelse(coviddat1$c10==58, NA, ifelse(coviddat1$c10==66, NA,ifelse(coviddat1$c10==98, NA,0)))))))
#Code for constructing mental health variables#
coviddat1$MHQ2aa<-coviddat1$MHQ2a
coviddat1$MHQ2aa[coviddat1$MHQ2a==1]<-2
coviddat1$MHQ2aa[coviddat1$MHQ2a==2]<-1
coviddat1$MHQ2ba<-coviddat1$MHQ2b
coviddat1$MHQ2ba[coviddat1$MHQ2b==1]<-2
coviddat1$MHQ2ba[coviddat1$MHQ2b==2]<-1
coviddat1$MHQ2ca<-coviddat1$MHQ2c
coviddat1$MHQ2ca[coviddat1$MHQ2c==1]<-2
coviddat1$MHQ2ca[coviddat1$MHQ2c==2]<-1
coviddat1$MHQ2da<-coviddat1$MHQ2d
coviddat1$MHQ2da[coviddat1$MHQ2d==1]<-2
coviddat1$MHQ2da[coviddat1$MHQ2d==2]<-1
coviddat1$MHQ2ea<-coviddat1$MHQ2e
coviddat1$MHQ2ea[coviddat1$MHQ2e==1]<-2
coviddat1$MHQ2ea[coviddat1$MHQ2e==2]<-1
coviddat1$MHQ2fa<-coviddat1$MHQ2f
coviddat1$MHQ2fa[coviddat1$MHQ2f==1]<-2
coviddat1$MHQ2fa[coviddat1$MHQ2f==2]<-1
coviddat1$MHQ2ga<-coviddat1$MHQ2g
coviddat1$MHQ2ga[coviddat1$MHQ2g==1]<-2
coviddat1$MHQ2ga[coviddat1$MHQ2g==2]<-1
coviddat1$MHQ2ha<-coviddat1$MHQ2h
coviddat1$MHQ2ha[coviddat1$MHQ2h==1]<-2
coviddat1$MHQ2ha[coviddat1$MHQ2h==2]<-1
coviddat1$MHQ2ia<-coviddat1$MHQ2i
coviddat1$MHQ2ia[coviddat1$MHQ2i==1]<-2
coviddat1$MHQ2ia[coviddat1$MHQ2i==2]<-1
coviddat1$MHQ2ja<-coviddat1$MHQ2j
coviddat1$MHQ2ja[coviddat1$MHQ2j==1]<-2
coviddat1$MHQ2ja[coviddat1$MHQ2j==2]<-1
mhq2all<-cbind(coviddat1$MHQ2aa, coviddat1$MHQ2ba, coviddat1$MHQ2ca, coviddat1$MHQ2da,coviddat1$MHQ2ea,coviddat1$MHQ2fa,coviddat1$MHQ2ga, coviddat1$MHQ2ha, coviddat1$MHQ2ia, coviddat1$MHQ2ja)
mhq2all %>% fa.parallel(fm="ml", cor="poly") 
fa1 <- fa(mhq2all,5,fm="ml" ,rotate="varimax", cor="poly")
summary(fa1)
coviddat1$MHQ2ind1a<- (coviddat1$MHQ2ea+coviddat1$MHQ2ga+coviddat1$MHQ2ha+coviddat1$MHQ2ia)/4
coviddat1$MHQ2ind2a<- (coviddat1$MHQ2ba+coviddat1$MHQ2ca)/2
#Note: MHQ2da is the third factor#
coviddat1$MHQ2ind3a<- (coviddat1$MHQ2aa+coviddat1$MHQ2fa)/2
#Note: MHQ2ja is the fifth factor#
#Code for constructing party alignment variable#
coviddat1$match<- 0
coviddat1$match[coviddat1$states==2]<- 1
coviddat1$match[coviddat1$states==3]<- 1
coviddat1$match[coviddat1$states==6]<- 1
coviddat1$match[coviddat1$states==7]<- 1
coviddat1$match[coviddat1$states==8]<- 1
coviddat1$match[coviddat1$states==9]<- 1
coviddat1$match[coviddat1$states==10]<- 1
coviddat1$match[coviddat1$states==12]<- 1
coviddat1$match[coviddat1$states==14]<- 1
coviddat1$match[coviddat1$states==16]<- 1
coviddat1$match[coviddat1$states==21]<- 1
coviddat1$match[coviddat1$states==22]<- 1
coviddat1$match[coviddat1$states==23]<- 1
#Code for constructing stringency index#
coviddat1$index_1<-0
coviddat1$index_1[coviddat1$states==1]<- -30.5
coviddat1$index_1[coviddat1$states==10]<- "NA"
coviddat1$index_1[coviddat1$states==11]<- -31
coviddat1$index_1[coviddat1$states==12]<- -20
coviddat1$index_1[coviddat1$states==13]<- -22
coviddat1$index_1[coviddat1$states==14]<- -31
coviddat1$index_1[coviddat1$states==15]<- -36
coviddat1$index_1[coviddat1$states==16]<- -17.5
coviddat1$index_1[coviddat1$states==17]<- -23
coviddat1$index_1[coviddat1$states==18]<- -42.5
coviddat1$index_1[coviddat1$states==19]<- -29
coviddat1$index_1[coviddat1$states==2]<- -17
coviddat1$index_1[coviddat1$states==20]<- -17
coviddat1$index_1[coviddat1$states==21]<- -45
coviddat1$index_1[coviddat1$states==22]<- -27
coviddat1$index_1[coviddat1$states==23]<- -30
coviddat1$index_1[coviddat1$states==24]<- -28
coviddat1$index_1[coviddat1$states==25]<- -25
coviddat1$index_1[coviddat1$states==3]<- -10
coviddat1$index_1[coviddat1$states==4]<- -27
coviddat1$index_1[coviddat1$states==5]<- -59.5
coviddat1$index_1[coviddat1$states==6]<- -42
coviddat1$index_1[coviddat1$states==7]<- -23
coviddat1$index_1[coviddat1$states==8]<- -42
coviddat1$index_1[coviddat1$states==9]<- -14
coviddat1$index_1<-as.numeric(coviddat1$index_1)
#Code for constructing asset ownership variable#
coviddat1$MR1_1a<-coviddat1$MR1_1
coviddat1$MR1_1a[coviddat1$MR1_1==1]<-2
coviddat1$MR1_1a[coviddat1$MR1_1==2]<-1
coviddat1$MR_2a<-coviddat1$MR_2
coviddat1$MR_2a[coviddat1$MR_2==1]<-2
coviddat1$MR_2a[coviddat1$MR_2==2]<-1
coviddat1$MR_3a<-coviddat1$MR_3
coviddat1$MR_3a[coviddat1$MR_3==1]<-2
coviddat1$MR_3a[coviddat1$MR_3==2]<-1
coviddat1$MR_4a<-coviddat1$MR_4
coviddat1$MR_4a[coviddat1$MR_4==1]<-2
coviddat1$MR_4a[coviddat1$MR_4==2]<-1
coviddat1$MR_5a<-coviddat1$MR_5
coviddat1$MR_5a[coviddat1$MR_5==1]<-2
coviddat1$MR_5a[coviddat1$MR_5==2]<-1
coviddat1$MR_6a<-coviddat1$MR_6
coviddat1$MR_6a[coviddat1$MR_6==1]<-2
coviddat1$MR_6a[coviddat1$MR_6==2]<-1
coviddat1$MR_7a<-coviddat1$MR_7
coviddat1$MR_7a[coviddat1$MR_7==1]<-2
coviddat1$MR_7a[coviddat1$MR_7==2]<-1
coviddat1$MR_8a<-coviddat1$MR_8
coviddat1$MR_8a[coviddat1$MR_8==1]<-2
coviddat1$MR_8a[coviddat1$MR_8==2]<-1
coviddat1$MR_9a<-coviddat1$MR_9
coviddat1$MR_9a[coviddat1$MR_9==1]<-2
coviddat1$MR_9a[coviddat1$MR_9==2]<-1
coviddat1$MR_10a<-coviddat1$MR_10
coviddat1$MR_10a[coviddat1$MR_10==1]<-2
coviddat1$MR_10a[coviddat1$MR_10==2]<-1
coviddat1$MR_11a<-coviddat1$MR_11
coviddat1$MR_11a[coviddat1$MR_11==1]<-2
coviddat1$MR_11a[coviddat1$MR_11==2]<-1
coviddat1$assetowna<-coviddat1$MR1_1a+coviddat1$MR_2a+coviddat1$MR_3a+coviddat1$MR_4a+coviddat1$MR_5a+coviddat1$MR_6a+coviddat1$MR_7a+coviddat1$MR_8a+coviddat1$MR_9a+coviddat1$MR_10a+coviddat1$MR_11a
#Code for constructing other independent variables#
coviddat1$gradunemp<-ifelse(coviddat1$C4==1, 1, 0)
coviddat1$inchigh<-ifelse(coviddat1$C5==7, 1, 0)
socialgrp<-coviddat1$C6
socialgrp[socialgrp>5]<- 6
socialgrp[socialgrp==0]<- 6
socialgrp<-as.factor(socialgrp)
coviddat1$socialgrp<-relevel(socialgrp, ref="4")
econhh<-coviddat1$c12a
econhh[econhh==0]<-2
econhh[econhh==5]<-2
econhh<-as.factor(econhh)
coviddat1$econhh<-relevel(econhh, ref="2")
coviddat1$migrant<-ifelse(coviddat1$c18==2,1,0)
coviddat1$Regions<-as.factor(coviddat1$Regions)
coviddat1$Regionsa<-relevel(coviddat1$Regions, ref="3")


#Wave 2#
Mental_health_wave_2_COVID_Eco_OB_MR_ <- read_sav("~/Downloads/Mental health wave-2 (COVID+Eco+OB+MR).sav")
coviddat2<-Mental_health_wave_2_COVID_Eco_OB_MR_
coviddat2<-as.data.frame(coviddat2)
coviddat2<-coviddat2[which(coviddat2$MHQ4a !=0 & coviddat2$MHQ4b!=0 & coviddat2$MHQ4c !=0 & coviddat2$MHQ4d !=0 & coviddat2$MHQ4e !=0 & coviddat2$MHQ4f !=0 & coviddat2$MHQ4g !=0 ),]
#Code for constructing dependent variable#
coviddat2$bjp2020<- ifelse(coviddat2$c10==12, 1, ifelse(coviddat2$c10==22, NA, ifelse(coviddat2$c10==28, NA, ifelse(coviddat2$c10==37, NA, ifelse(coviddat2$c10==45, NA, ifelse(coviddat2$c10==58, NA, ifelse(coviddat2$c10==66, NA,ifelse(coviddat2$c10==98, NA,0))))))))
#Code for constructing mental health variables#
coviddat2$MHQ4aa<-coviddat2$MHQ4a
coviddat2$MHQ4aa[coviddat2$MHQ4a==1]<-5
coviddat2$MHQ4aa[coviddat2$MHQ4a==2]<-4
coviddat2$MHQ4aa[coviddat2$MHQ4a==3]<-3
coviddat2$MHQ4aa[coviddat2$MHQ4a==4]<-2
coviddat2$MHQ4aa[coviddat2$MHQ4a==5]<-1
coviddat2$MHQ4ba<-coviddat2$MHQ4b
coviddat2$MHQ4ba[coviddat2$MHQ4b==1]<-5
coviddat2$MHQ4ba[coviddat2$MHQ4b==2]<-4
coviddat2$MHQ4ba[coviddat2$MHQ4b==3]<-3
coviddat2$MHQ4ba[coviddat2$MHQ4b==4]<-2
coviddat2$MHQ4ba[coviddat2$MHQ4b==5]<-1
coviddat2$MHQ4ca<-coviddat2$MHQ4c
coviddat2$MHQ4ca[coviddat2$MHQ4c==1]<-5
coviddat2$MHQ4ca[coviddat2$MHQ4c==2]<-4
coviddat2$MHQ4ca[coviddat2$MHQ4c==3]<-3
coviddat2$MHQ4ca[coviddat2$MHQ4c==4]<-2
coviddat2$MHQ4ca[coviddat2$MHQ4c==5]<-1
coviddat2$MHQ4da<-coviddat2$MHQ4d
coviddat2$MHQ4da[coviddat2$MHQ4d==1]<-5
coviddat2$MHQ4da[coviddat2$MHQ4d==2]<-4
coviddat2$MHQ4da[coviddat2$MHQ4d==3]<-3
coviddat2$MHQ4da[coviddat2$MHQ4d==4]<-2
coviddat2$MHQ4da[coviddat2$MHQ4d==5]<-1
coviddat2$MHQ4ea<-coviddat2$MHQ4e
coviddat2$MHQ4ea[coviddat2$MHQ4e==1]<-5
coviddat2$MHQ4ea[coviddat2$MHQ4e==2]<-4
coviddat2$MHQ4ea[coviddat2$MHQ4e==3]<-3
coviddat2$MHQ4ea[coviddat2$MHQ4e==4]<-2
coviddat2$MHQ4ea[coviddat2$MHQ4e==5]<-1
coviddat2$MHQ4fa<-coviddat2$MHQ4f
coviddat2$MHQ4fa[coviddat2$MHQ4f==1]<-5
coviddat2$MHQ4fa[coviddat2$MHQ4f==2]<-4
coviddat2$MHQ4fa[coviddat2$MHQ4f==3]<-3
coviddat2$MHQ4fa[coviddat2$MHQ4f==4]<-2
coviddat2$MHQ4fa[coviddat2$MHQ4f==5]<-1
coviddat2$MHQ4ga<-coviddat2$MHQ4g
coviddat2$MHQ4ga[coviddat2$MHQ4g==1]<-5
coviddat2$MHQ4ga[coviddat2$MHQ4g==2]<-4
coviddat2$MHQ4ga[coviddat2$MHQ4g==3]<-3
coviddat2$MHQ4ga[coviddat2$MHQ4g==4]<-2
coviddat2$MHQ4ga[coviddat2$MHQ4g==5]<-1
mhq3all<-cbind(coviddat2$MHQ4aa, coviddat2$MHQ4ba, coviddat2$MHQ4ca, coviddat2$MHQ4da,coviddat2$MHQ4ea,coviddat2$MHQ4fa,coviddat2$MHQ4ga)
mhq3all %>% fa.parallel(fm="ml", cor="poly") 
fa1 <- fa(mhq3all,2,fm="ml" ,rotate="varimax", cor="poly")
summary(fa1)
coviddat2$MHQ4ind1a<- (coviddat2$MHQ4ba+ coviddat2$MHQ4da+coviddat2$MHQ4ea+coviddat2$MHQ4fa)/4
coviddat2$MHQ4ind2a<- (coviddat2$MHQ4aa+ coviddat2$MHQ4ca + coviddat2$MHQ4ga)/3
coviddat2$MHQ4inda<- (coviddat2$MHQ4ba+ coviddat2$MHQ4da+coviddat2$MHQ4ea+coviddat2$MHQ4fa-coviddat2$MHQ4aa-coviddat2$MHQ4ca-coviddat2$MHQ4ga)/7
#Code for constructing party alignment variable#
coviddat2$match<- 0
coviddat2$match[coviddat2$c18==2]<- 1
coviddat2$match[coviddat2$c18==3]<- 1
coviddat2$match[coviddat2$c18==6]<- 1
coviddat2$match[coviddat2$c18==7]<- 1
coviddat2$match[coviddat2$c18==8]<- 1
coviddat2$match[coviddat2$c18==9]<- 1
coviddat2$match[coviddat2$c18==10]<- 1
coviddat2$match[coviddat2$c18==12]<- 1
coviddat2$match[coviddat2$c18==14]<- 1
coviddat2$match[coviddat2$c18==16]<- 1
coviddat2$match[coviddat2$c18==21]<- 1
coviddat2$match[coviddat2$c18==22]<- 1
coviddat2$match[coviddat2$c18==23]<- 1
#Code for creating stringency_index#
coviddat2$index_2<-0
coviddat2$index_2[coviddat2$c18==1]<- -29
coviddat2$index_2[coviddat2$c18==10]<- "NA"
coviddat2$index_2[coviddat2$c18==11]<- -26
coviddat2$index_2[coviddat2$c18==12]<- -16
coviddat2$index_2[coviddat2$c18==13]<- -22
coviddat2$index_2[coviddat2$c18==14]<- -26
coviddat2$index_2[coviddat2$c18==15]<- -34
coviddat2$index_2[coviddat2$c18==16]<- -21
coviddat2$index_2[coviddat2$c18==17]<- -21
coviddat2$index_2[coviddat2$c18==18]<- -42
coviddat2$index_2[coviddat2$c18==19]<- -25
coviddat2$index_2[coviddat2$c18==2]<- -14
coviddat2$index_2[coviddat2$c18==20]<- -16
coviddat2$index_2[coviddat2$c18==21]<- -44.25
coviddat2$index_2[coviddat2$c18==22]<- -22
coviddat2$index_2[coviddat2$c18==23]<- -26.5
coviddat2$index_2[coviddat2$c18==24]<- -23
coviddat2$index_2[coviddat2$c18==25]<- -20
coviddat2$index_2[coviddat2$c18==3]<- -7
coviddat2$index_2[coviddat2$c18==4]<- -24
coviddat2$index_2[coviddat2$c18==5]<- -57.5
coviddat2$index_2[coviddat2$c18==6]<- -44
coviddat2$index_2[coviddat2$c18==7]<- -20
coviddat2$index_2[coviddat2$c18==8]<- -40
coviddat2$index_2[coviddat2$c18==9]<- -13
coviddat2$index_2<-as.numeric(coviddat2$index_2)
#Code for creating asset ownership variable#
coviddat2$MR1_1a<-coviddat2$MR1_1
coviddat2$MR1_1a[coviddat2$MR1_1==1]<-2
coviddat2$MR1_1a[coviddat2$MR1_1==2]<-1
coviddat2$MR_2a<-coviddat2$MR_2
coviddat2$MR_2a[coviddat2$MR_2==1]<-2
coviddat2$MR_2a[coviddat2$MR_2==2]<-1
coviddat2$MR_3a<-coviddat2$MR_3
coviddat2$MR_3a[coviddat2$MR_3==1]<-2
coviddat2$MR_3a[coviddat2$MR_3==2]<-1
coviddat2$MR_4a<-coviddat2$MR_4
coviddat2$MR_4a[coviddat2$MR_4==1]<-2
coviddat2$MR_4a[coviddat2$MR_4==2]<-1
coviddat2$MR_5a<-coviddat2$MR_5
coviddat2$MR_5a[coviddat2$MR_5==1]<-2
coviddat2$MR_5a[coviddat2$MR_5==2]<-1
coviddat2$MR_6a<-coviddat2$MR_6
coviddat2$MR_6a[coviddat2$MR_6==1]<-2
coviddat2$MR_6a[coviddat2$MR_6==2]<-1
coviddat2$MR_7a<-coviddat2$MR_7
coviddat2$MR_7a[coviddat2$MR_7==1]<-2
coviddat2$MR_7a[coviddat2$MR_7==2]<-1
coviddat2$MR_8a<-coviddat2$MR_8
coviddat2$MR_8a[coviddat2$MR_8==1]<-2
coviddat2$MR_8a[coviddat2$MR_8==2]<-1
coviddat2$MR_9a<-coviddat2$MR_9
coviddat2$MR_9a[coviddat2$MR_9==1]<-2
coviddat2$MR_9a[coviddat2$MR_9==2]<-1
coviddat2$MR_10a<-coviddat2$MR_10
coviddat2$MR_10a[coviddat2$MR_10==1]<-2
coviddat2$MR_10a[coviddat2$MR_10==2]<-1
coviddat2$MR_11a<-coviddat2$MR_11
coviddat2$MR_11a[coviddat2$MR_11==1]<-2
coviddat2$MR_11a[coviddat2$MR_11==2]<-1
coviddat2$assetowna<-coviddat2$MR1_1a+coviddat2$MR_2a+coviddat2$MR_3a+coviddat2$MR_4a+coviddat2$MR_5a+coviddat2$MR_6a+coviddat2$MR_7a+coviddat2$MR_8a+coviddat2$MR_9a+coviddat2$MR_10a+coviddat2$MR_11a
#Code for creating other dependent variables#
coviddat2$gradunemp<-ifelse(coviddat2$C4==1, 1, 0)
coviddat2$inchigh<-ifelse(coviddat2$C5==7, 1, 0)
socialgrp<-coviddat2$C6
socialgrp[socialgrp>5]<- 6
socialgrp[socialgrp==0]<- 6
socialgrp<-as.factor(socialgrp)
coviddat2$socialgrp<-relevel(socialgrp, ref="4")
econhh<-coviddat2$c12a
econhh[econhh==0]<-2
econhh[econhh==5]<-2
econhh<-as.factor(econhh)
coviddat2$econhh<-relevel(econhh, ref="2")
coviddat2$migrant<-ifelse(coviddat2$c18==2,1,0)
coviddat2$Regions<-as.factor(coviddat2$Regions)
coviddat2$Regionsa<-relevel(coviddat2$Regions, ref="3")

#Wave 3#
Mental_health_wave_3_COVID_Eco_OB_MR_ <- read_sav("~/Downloads/Mental health wave-3 (COVID+Eco+OB+MR).sav")
coviddat3<-Mental_health_wave_3_COVID_Eco_OB_MR_
coviddat3<-as.data.frame(coviddat3)
coviddat3<-coviddat3[which(coviddat3$MHQ5a !=0 & coviddat3$MHQ5b!=0 & coviddat3$MHQ5c !=0 & coviddat3$MHQ5d !=0 & coviddat3$MHQ5e !=0 & coviddat3$MHQ5f !=0 & coviddat3$MHQ5g !=0 & coviddat3$MHQ5h !=0),]
#Code for constructing dependent variable#
coviddat3$bjp2020<- ifelse(coviddat3$c10==12, 1, ifelse(coviddat3$c10==22, NA, ifelse(coviddat3$c10==24, NA,ifelse(coviddat3$c10==28, NA, ifelse(coviddat3$c10==37, NA, ifelse(coviddat3$c10==45, NA, ifelse(coviddat3$c10==58, NA, ifelse(coviddat3$c10==66, NA,ifelse(coviddat3$c10==98, NA,0)))))))))
#Code for constructing mental health variables#
coviddat3$MHQ5aa<-coviddat3$MHQ5a
coviddat3$MHQ5aa[coviddat3$MHQ5a==1]<-3
coviddat3$MHQ5aa[coviddat3$MHQ5a==2]<-2
coviddat3$MHQ5aa[coviddat3$MHQ5a==3]<-1
coviddat3$MHQ5ba<-coviddat3$MHQ5b
coviddat3$MHQ5ba[coviddat3$MHQ5b==1]<-3
coviddat3$MHQ5ba[coviddat3$MHQ5b==2]<-2
coviddat3$MHQ5ba[coviddat3$MHQ5b==3]<-1
coviddat3$MHQ5ca<-coviddat3$MHQ5c
coviddat3$MHQ5ca[coviddat3$MHQ5c==1]<-3
coviddat3$MHQ5ca[coviddat3$MHQ5c==2]<-2
coviddat3$MHQ5ca[coviddat3$MHQ5c==3]<-1
coviddat3$MHQ5da<-coviddat3$MHQ5d
coviddat3$MHQ5da[coviddat3$MHQ5d==1]<-3
coviddat3$MHQ5da[coviddat3$MHQ5d==2]<-2
coviddat3$MHQ5da[coviddat3$MHQ5d==3]<-1
coviddat3$MHQ5ea<-coviddat3$MHQ5e
coviddat3$MHQ5ea[coviddat3$MHQ5e==1]<-3
coviddat3$MHQ5ea[coviddat3$MHQ5e==2]<-2
coviddat3$MHQ5ea[coviddat3$MHQ5e==3]<-1
coviddat3$MHQ5fa<-coviddat3$MHQ5f
coviddat3$MHQ5fa[coviddat3$MHQ5f==1]<-3
coviddat3$MHQ5fa[coviddat3$MHQ5f==2]<-2
coviddat3$MHQ5fa[coviddat3$MHQ5f==3]<-1
coviddat3$MHQ5ga<-coviddat3$MHQ5g
coviddat3$MHQ5ga[coviddat3$MHQ5g==1]<-3
coviddat3$MHQ5ga[coviddat3$MHQ5g==2]<-2
coviddat3$MHQ5ga[coviddat3$MHQ5g==3]<-1
coviddat3$MHQ5ha<-coviddat3$MHQ5h
coviddat3$MHQ5ha[coviddat3$MHQ5h==1]<-3
coviddat3$MHQ5ha[coviddat3$MHQ5h==2]<-2
coviddat3$MHQ5ha[coviddat3$MHQ5h==3]<-1
mhq4all<-cbind(coviddat3$MHQ5aa, coviddat3$MHQ5ba, coviddat3$MHQ5ca, coviddat3$MHQ5da,coviddat3$MHQ5ea,coviddat3$MHQ5fa,coviddat3$MHQ5ga, coviddat3$MHQ5ha)
mhq4all %>% fa.parallel(fm="ml", cor="poly") 
fa1 <- fa(mhq4all,4,fm="ml" ,rotate="varimax", cor="poly")
summary(fa1)
coviddat3$MHQ5ind1a<- (coviddat3$MHQ5aa+ coviddat3$MHQ5ba)/2
coviddat3$MHQ5ind2a<- (coviddat3$MHQ5ca+ coviddat3$MHQ5ga+coviddat3$MHQ5ha)/3
#Note: MHQ5fa is the third factor#
coviddat3$MHQ5ind3a<- (coviddat3$MHQ5da+ coviddat3$MHQ5ea)/2
#Code for constructing partisan alignment variable#
coviddat3$match<- 0
coviddat3$match[coviddat3$states==2]<- 1
coviddat3$match[coviddat3$states==3]<- 1
coviddat3$match[coviddat3$states==6]<- 1
coviddat3$match[coviddat3$states==7]<- 1
coviddat3$match[coviddat3$states==8]<- 1
coviddat3$match[coviddat3$states==9]<- 1
coviddat3$match[coviddat3$states==10]<- 1
coviddat3$match[coviddat3$states==12]<- 1
coviddat3$match[coviddat3$states==14]<- 1
coviddat3$match[coviddat3$states==16]<- 1
coviddat3$match[coviddat3$states==21]<- 1
coviddat3$match[coviddat3$states==22]<- 1
coviddat3$match[coviddat3$states==23]<- 1
#Code for creating stringency index#
coviddat3$index_3<-0
coviddat3$index_3[coviddat3$states==1]<- -30
coviddat3$index_3[coviddat3$states==10]<- "NA"
coviddat3$index_3[coviddat3$states==11]<- -25
coviddat3$index_3[coviddat3$states==12]<- -19
coviddat3$index_3[coviddat3$states==13]<- -20
coviddat3$index_3[coviddat3$states==14]<- -26
coviddat3$index_3[coviddat3$states==15]<- -35
coviddat3$index_3[coviddat3$states==16]<- -19
coviddat3$index_3[coviddat3$states==17]<- -24
coviddat3$index_3[coviddat3$states==18]<- -42
coviddat3$index_3[coviddat3$states==19]<- -24
coviddat3$index_3[coviddat3$states==2]<- -15
coviddat3$index_3[coviddat3$states==20]<- -20
coviddat3$index_3[coviddat3$states==21]<- -48.5
coviddat3$index_3[coviddat3$states==22]<- -24
coviddat3$index_3[coviddat3$states==23]<- -28
coviddat3$index_3[coviddat3$states==24]<- -21
coviddat3$index_3[coviddat3$states==25]<- -26
coviddat3$index_3[coviddat3$states==3]<- -9
coviddat3$index_3[coviddat3$states==4]<- -24
coviddat3$index_3[coviddat3$states==5]<- -59
coviddat3$index_3[coviddat3$states==6]<- -45
coviddat3$index_3[coviddat3$states==7]<- -19
coviddat3$index_3[coviddat3$states==8]<- -40
coviddat3$index_3[coviddat3$states==9]<- -13
coviddat3$index_3<-as.numeric(coviddat3$index_3)
#Code for constructing asset ownership variable#
coviddat3$MR1_1a<-coviddat3$MR1_1
coviddat3$MR1_1a[coviddat3$MR1_1==1]<-2
coviddat3$MR1_1a[coviddat3$MR1_1==2]<-1
coviddat3$MR_2a<-coviddat3$MR_2
coviddat3$MR_2a[coviddat3$MR_2==1]<-2
coviddat3$MR_2a[coviddat3$MR_2==2]<-1
coviddat3$MR_3a<-coviddat3$MR_3
coviddat3$MR_3a[coviddat3$MR_3==1]<-2
coviddat3$MR_3a[coviddat3$MR_3==2]<-1
coviddat3$MR_4a<-coviddat3$MR_4
coviddat3$MR_4a[coviddat3$MR_4==1]<-2
coviddat3$MR_4a[coviddat3$MR_4==2]<-1
coviddat3$MR_5a<-coviddat3$MR_5
coviddat3$MR_5a[coviddat3$MR_5==1]<-2
coviddat3$MR_5a[coviddat3$MR_5==2]<-1
coviddat3$MR_6a<-coviddat3$MR_6
coviddat3$MR_6a[coviddat3$MR_6==1]<-2
coviddat3$MR_6a[coviddat3$MR_6==2]<-1
coviddat3$MR_7a<-coviddat3$MR_7
coviddat3$MR_7a[coviddat3$MR_7==1]<-2
coviddat3$MR_7a[coviddat3$MR_7==2]<-1
coviddat3$MR_8a<-coviddat3$MR_8
coviddat3$MR_8a[coviddat3$MR_8==1]<-2
coviddat3$MR_8a[coviddat3$MR_8==2]<-1
coviddat3$MR_9a<-coviddat3$MR_9
coviddat3$MR_9a[coviddat3$MR_9==1]<-2
coviddat3$MR_9a[coviddat3$MR_9==2]<-1
coviddat3$MR_10a<-coviddat3$MR_10
coviddat3$MR_10a[coviddat3$MR_10==1]<-2
coviddat3$MR_10a[coviddat3$MR_10==2]<-1
coviddat3$MR_11a<-coviddat3$MR_11
coviddat3$MR_11a[coviddat3$MR_11==1]<-2
coviddat3$MR_11a[coviddat3$MR_11==2]<-1
coviddat3$assetowna<-coviddat3$MR1_1a+coviddat3$MR_2a+coviddat3$MR_3a+coviddat3$MR_4a+coviddat3$MR_5a+coviddat3$MR_6a+coviddat3$MR_7a+coviddat3$MR_8a+coviddat3$MR_9a+coviddat3$MR_10a+coviddat3$MR_11a
#Code for constructing other independent variables#
coviddat3$gradunemp<-ifelse(coviddat3$C4==1, 1, 0)
coviddat3$inchigh<-ifelse(coviddat3$C5==7, 1, 0)
socialgrp<-coviddat3$C6
socialgrp[socialgrp>5]<- 6
socialgrp[socialgrp==0]<- 6
socialgrp<-as.factor(socialgrp)
coviddat3$socialgrp<-relevel(socialgrp, ref="4")
econhh<-coviddat3$c12a
econhh[econhh==0]<-2
econhh[econhh==4]<-2
econhh[econhh==5]<-2
econhh<-as.factor(econhh)
coviddat3$econhh<-relevel(econhh, ref="2")
coviddat3$migrant<-ifelse(coviddat3$c18==2,1,0)
coviddat3$bjp2020<- ifelse(coviddat3$c10==12, 1, ifelse(coviddat3$c10==22, NA, ifelse(coviddat3$c10==24, NA,ifelse(coviddat3$c10==28, NA, ifelse(coviddat3$c10==37, NA, ifelse(coviddat3$c10==45, NA, ifelse(coviddat3$c10==58, NA, ifelse(coviddat3$c10==66, NA,ifelse(coviddat3$c10==98, NA,0)))))))))
coviddat3$Regions<-as.factor(coviddat3$Regions)
coviddat3$Regionsa<-relevel(coviddat3$Regions, ref="3")

#Tables 1-3 created by summarizing all variables using describe function in psych package#

#Code for replicating Table 4#
fit<-glm(bjp2020~MHQ2ind1a+MHQ2ind2a+MHQ2da+MHQ2ind3a+MHQ2ja+C1+C2+C3+gradunemp+assetowna+socialgrp+econhh+migrant+index_1+ Regionsa, data=coviddat1, weights=wt, family="binomial")
fit<-glm(bjp2020~MHQ4inda+C1+C2+C3+gradunemp+assetowna+socialgrp+econhh+migrant+index_2+Regionsa, data=coviddat2, weights=wt, family="binomial")
fit<-glm(bjp2020~MHQ5ind1a+MHQ5ind2a+MHQ5fa+MHQ5ind3a+C1+C2+C3+gradunemp+assetowna+socialgrp+econhh+migrant+index_3+Regionsa, data=coviddat3, weights=wt,family="binomial") 


#Code for replicating Table 5#
fit<-glm(bjp2020~C1+C2+C3+gradunemp+assetowna+socialgrp+econhh+migrant+index_1+Regionsa+MHQ2ind1a*match+MHQ2ind2a*match+MHQ2da*match+MHQ2ind3a*match+MHQ2ja*match, data=coviddat1, weights=wt, family="binomial")
fit<-glm(bjp2020~MHQ4inda+match+C1+C2+C3+gradunemp+assetowna+socialgrp+econhh+migrant+index_2+Regionsa+MHQ4inda:match, data=coviddat2, weights=wt, family="binomial")
fit<-glm(bjp2020~MHQ5ind1a+MHQ5ind2a+MHQ5fa+MHQ5ind3a+match+C1+C2+C3+gradunemp+assetowna+socialgrp+econhh+migrant+index_3+Regionsa+MHQ5ind1a:match+MHQ5ind2a:match+MHQ5fa:match+MHQ5ind3a:match, data=coviddat3, weights=wt,family="binomial") 

#Code for replicating Figure 1#
coviddat1$MHQ2da<-as.factor(coviddat1$MHQ2da)
fit<-glm(bjp2020~MHQ2ind1a+MHQ2ind2a+MHQ2da+MHQ2ind3a+MHQ2ja+C1+C2+C3+gradunemp+assetowna+socialgrp+econhh+migrant+index_1+ Regionsa, data=coviddat1, weights=wt, family="binomial")
interact_plot(model = fit, pred = MHQ2da, modx = match, interval = TRUE, int.type = "confidence", x.label="Mental stress from loss of freedom", legend.main="CM alignment", y.label="Average predicted chance of voting BJP if elections held today", geom=c("point", "bar"))

#Code for replicating Figure 2#
fit<-glm(bjp2020~MHQ4inda+match+C1+C2+C3+gradunemp+assetowna+socialgrp+econhh+migrant+index_2+Regionsa+MHQ4inda:match, data=coviddat2, weights=wt, family="binomial")
interact_plot(model = fit, pred = MHQ4inda, modx = match, interval = TRUE, int.type = "confidence", x.label="Worsening mental health", legend.main="CM alignment", y.label="Average predicted chance of voting for BJP if elections held today")

#Code for replicating Figure 3#
fit<-glm(bjp2020~MHQ5ind1a+MHQ5ind2a+MHQ5fa+MHQ5ind3a+match+C1+C2+C3+gradunemp+assetowna+socialgrp+econhh+migrant+index_3+Regionsa+MHQ5ind1a:match+MHQ5ind2a:match+MHQ5fa:match+MHQ5ind3a:match, data=coviddat3, weights=wt,family="binomial") 
interact_plot(model = fit, pred = MHQ5ind1a, modx = match, interval = TRUE, int.type = "confidence", x.label="Coping by talking about mental health among family/friends/relatives", legend.main="CM alignment", y.label="Average predicted chance of voting for BJP if elections held today")

#Code for replicating Figure 4#
fit<-glm(bjp2020~MHQ5ind1a+MHQ5ind2a+MHQ5fa+MHQ5ind3a+match+C1+C2+C3+gradunemp+assetowna+socialgrp+econhh+migrant+index_3+Regionsa+MHQ5ind1a:match+MHQ5ind2a:match+MHQ5fa:match+MHQ5ind3a:match, data=coviddat3, weights=wt,family="binomial") 
interact_plot(model = fit, pred = MHQ5ind2a, modx = match, interval = TRUE, int.type = "confidence", x.label="Coping by checking on the internet/seeking professional medical help/trying home remedies", legend.main="CM alignment", y.label="Average predicted chance of voting for BJP if elections held today")

#Code for replicating Figure 5#
fit<-glm(bjp2020~MHQ5ind1a+MHQ5ind2a+MHQ5fa+MHQ5ind3a+match+C1+C2+C3+gradunemp+assetowna+socialgrp+econhh+migrant+index_3+Regionsa+MHQ5ind1a:match+MHQ5ind2a:match+MHQ5fa:match+MHQ5ind3a:match, data=coviddat3, weights=wt,family="binomial") 
interact_plot(model = fit, pred = MHQ5fa, modx = match, interval = TRUE, int.type = "confidence", x.label="Coping by trying to keep busy with hobbies", legend.main="CM alignment", y.label="Average predicted chance of voting for BJP if elections held today")

#Code for replicating Figure 6#
fit<-glm(bjp2020~MHQ5ind1a+MHQ5ind2a+MHQ5fa+MHQ5ind3a+match+C1+C2+C3+gradunemp+assetowna+socialgrp+econhh+migrant+index_3+Regionsa+MHQ5ind1a:match+MHQ5ind2a:match+MHQ5fa:match+MHQ5ind3a:match, data=coviddat3, weights=wt,family="binomial") 
interact_plot(model = fit, pred = MHQ5ind3a, modx = match, interval = TRUE, int.type = "confidence", x.label="Coping by listening to motivational talks/meditating", legend.main="CM alignment", y.label="Average predicted chance of voting for BJP if elections held today")
